package com.cqndt.disaster.device.service.impl;

import java.util.*;
import java.io.File;
import java.io.IOException;
import com.cqndt.disaster.device.dao.*;
import com.cqndt.disaster.device.domain.*;
import com.cqndt.disaster.device.util.Result;
import org.springframework.stereotype.Service;
import com.cqndt.disaster.device.vo.TabProjectVo;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Value;
import com.cqndt.disaster.device.service.TabProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class TabProjectServiceImpl extends ServiceImpl<TabProjectMapper,
		TabProject> implements TabProjectService {
    @Value("${upload-path}")
    private String uploadPath;
    @Autowired
    private TabProjectMapper tabProjectMapper;
    @Autowired
    private TabProjectCjdwMapper tabProjectCjdwMapper;
    @Autowired
    private TabProjectXmbzMapper tabProjectXmbzMapper;
    @Autowired
    private TabProjectDatumMapper tabProjectDatumMapper;
    @Autowired
    private TabStaticTypeMapper tabStaticTypeMapper;
    @Autowired
    private TabDeviceCheckMapper tabDeviceCheckMapper;
    
    @Override
    public TabProject getById(Integer projectId) {
        return tabProjectMapper.selectByPrimaryKey(projectId);
    }
    @Override
    public List<TabProjectVo> selectByCondition(TabProjectVo vo){
        return tabProjectMapper.selectByCondition(vo);
    }
    @Override
    public List<TabProjectCjdw> selectCjdwByProjectId(Integer projectId){
        return tabProjectCjdwMapper.selectCjdwByProjectId(projectId);
    }
    @Override
    public List<TabProjectXmbz> selectXmbzByProjectId(Integer projectId){
        return tabProjectXmbzMapper.selectXmbzByProjectId(projectId);
    }
    @Override
    public List<Map<String, Object>> selectProjectDatum(Integer projectId) {
        return  getAllDatumList(projectId);
    }
    /**
     * @return 获取所有文档
     **/
    private List<Map<String,Object>> getAllDatumList(Integer projectId){
        //查询根文档
        List<Map<String,Object>> datumList = tabProjectDatumMapper.selectDatumForProject(0,projectId);
        //递归获取子菜单
        getDatumTreeList(datumList,projectId);
        return datumList;
    }
    /**
     * @return 递归
     **/
    private List<Map<String,Object>> getDatumTreeList(List<Map<String,Object>> datumList,Integer projectId){
        List<Map<String,Object>> subMenuList = new ArrayList<Map<String,Object>>();
        for(Map<String,Object> map : datumList){
            //目录
            if("1".equals(map.get("file_type"))){
                List<Map<String,Object>> list = tabProjectDatumMapper.selectDatumForProject((Integer)map.get("id"),projectId);
                map.put("children",getDatumTreeList(list,projectId));
            }else{
                map.put("children",new ArrayList<Map<String,Object>>());
            }
            subMenuList.add(map);
        }
        return subMenuList;
    }
    @Override
    public List<Map<String,Object>> selectByUserId(Integer userId){
        return tabProjectMapper.selectByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> getDeviceByProjectId(Integer projectId) {
        List<TabStaticType> list = tabStaticTypeMapper.selectByStaticNum("11");
        //离线时间判断
        Integer num = list.get(0).getStaticKeyval();
        return tabProjectMapper.getDeviceByProjectId(projectId,num);
    }

    @Override
    public Map<String, Object> getDeviceNumByProjectId(Integer projectId) {
        List<TabStaticType> list = tabStaticTypeMapper.selectByStaticNum("11");
        //离线时间判断
        Integer num = list.get(0).getStaticKeyval();
        return tabProjectMapper.getDeviceNumByProjectId(projectId,num);
    }

    @Override
    public Result uploadProjectImg(MultipartFile file, Integer projectId) {

        File cfile = new File(uploadPath+File.separator+"img"+File.separator+"project"+File.separator);
        if (!cfile.exists()){
            cfile.mkdirs();
        }
        //获取图片名称
        String fileName = file.getOriginalFilename();
        //图片后缀
        String newFileName = "project/"+UUID.randomUUID()+fileName.substring(fileName.lastIndexOf("."));
        //存储图片
        try {
            file.transferTo(new File(uploadPath+File.separator+"img"+File.separator+newFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        TabAttachment tabAttachment = new TabAttachment();
        tabAttachment.setAttachmentDate(new Date());
        tabAttachment.setAttachmentName(fileName);
        tabAttachment.setAttachmentUrl(newFileName);
        tabAttachment.setAttachmentDesc("项目图片");
        tabAttachment.setAttachmentType("1");
        tabDeviceCheckMapper.insertTabAttachmentSelective(tabAttachment);
        TabProject tabProject = tabProjectMapper.selectByPrimaryKey(projectId);
        if(tabProject == null){
            return new Result().failure(-1, "项目信息为空");
        }
        String projectImg = tabProject.getProjectImg();
        if(projectImg == null || "".equals(projectImg)){
            tabProject.setProjectImg(String.valueOf(tabAttachment.getId()));
        } else {
            tabProject.setProjectImg(tabProject.getProjectImg() + "," + tabAttachment.getId());
        }
        tabProjectMapper.updateByPrimaryKey(tabProject);
        Result result = new Result();
        result.setCode(200);
        return result;
    }
}